package com.orharlan.carassistant.features;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

import com.orharlan.carassistant.R;
import com.orharlan.carassistant.datamodel.Datamodel;
import android.content.Context;
import android.util.Xml;

public class PersistenceXML
{
	private Context context;
	private Datamodel data;

	final String xmlFile = "dataCarAssistant";

	public PersistenceXML( Context context )
	{
		this.context = context;
		data = Datamodel.singleton( );
	}

	// Persistence
	public void setDataStorage( )
	{

		try
		{

			FileOutputStream fileos = context.getApplicationContext( ).openFileOutput( xmlFile, Context.MODE_PRIVATE );
			XmlSerializer xmlSerializer = Xml.newSerializer( );
			StringWriter writer = new StringWriter( );
			xmlSerializer.setOutput( writer );
			xmlSerializer.startDocument( "UTF-8", true );
			xmlSerializer.startTag( null, "GPSData" );

			xmlSerializer.startTag( null, "carLatitude" );
			xmlSerializer.text( data.getCarLatitude( ) + "" );
			xmlSerializer.endTag( null, "carLatitude" );

			xmlSerializer.startTag( null, "carLongitude" );
			xmlSerializer.text( data.getCarLongitude( ) + "" );
			xmlSerializer.endTag( null, "carLongitude" );

			xmlSerializer.startTag( null, "homeLatitude" );
			xmlSerializer.text( data.getHomeLatitude( ) + "" );
			xmlSerializer.endTag( null, "homeLatitude" );

			xmlSerializer.startTag( null, "homeLongitude" );
			xmlSerializer.text( data.getHomeLongitude( ) + "" );
			xmlSerializer.endTag( null, "homeLongitude" );

			xmlSerializer.startTag( null, "jobLatitude" );
			xmlSerializer.text( data.getJobLatitude( ) + "" );
			xmlSerializer.endTag( null, "jobLatitude" );

			xmlSerializer.startTag( null, "jobLongitude" );
			xmlSerializer.text( data.getJobLongitude( ) + "" );
			xmlSerializer.endTag( null, "jobLongitude" );

			xmlSerializer.startTag( null, "mailAddress" );
			xmlSerializer.text( data.getMailAddress( ) );
			xmlSerializer.endTag( null, "mailAddress" );

			xmlSerializer.endTag( null, "GPSData" );
			xmlSerializer.endDocument( );
			xmlSerializer.flush( );
			String dataWrite = writer.toString( );
			fileos.write( dataWrite.getBytes( ) );
			fileos.close( );
		}
		catch( FileNotFoundException e )
		{

			e.printStackTrace( );
		}
		catch( IllegalArgumentException e )
		{
			// TODO Auto-generated catch block
			e.printStackTrace( );
		}
		catch( IllegalStateException e )
		{
			// TODO Auto-generated catch block
			e.printStackTrace( );
		}
		catch( IOException e )
		{
			// TODO Auto-generated catch block
			e.printStackTrace( );
		}
	}

	public void getDataStorage( )
	{

		ArrayList< String > gpsData = new ArrayList< String >( );
		String receivedText = "";
		try
		{
			FileInputStream fis = context.getApplicationContext( ).openFileInput( xmlFile );
			InputStreamReader isr = new InputStreamReader( fis );
			char[ ] inputBuffer = new char[ fis.available( ) ];
			isr.read( inputBuffer );
			receivedText = new String( inputBuffer );
			isr.close( );
			fis.close( );
		}
		catch( FileNotFoundException e3 )
		{
			// By the initial start of the application will occur this exception
			System.out.println( context.getString( R.string.noFile ) );
			e3.printStackTrace( );
		}
		catch( IOException e )
		{
			// TODO Auto-generated catch block
			e.printStackTrace( );
		}
		XmlPullParserFactory factory = null;
		try
		{
			factory = XmlPullParserFactory.newInstance( );
		}
		catch( XmlPullParserException e2 )
		{
			// TODO Auto-generated catch block
			e2.printStackTrace( );
		}

		// Specifies on with the value true that the parser produced by this
		// factory will provide support for XML
		factory.setNamespaceAware( true );
		XmlPullParser xpp = null;
		try
		{

			// Create a new instance of a XML Pull Parser
			xpp = factory.newPullParser( );
		}
		catch( XmlPullParserException e2 )
		{
			// This exception is thrown to signal XML Pull Parser related faults
			e2.printStackTrace( );
		}
		try
		{
			// Sets the input text for the Parser
			xpp.setInput( new StringReader( receivedText ) );
		}
		catch( XmlPullParserException e1 )
		{
			// TODO Auto-generated catch block
			e1.printStackTrace( );
		}
		int eventType = 0;
		try
		{
			eventType = xpp.getEventType( );
		}
		catch( XmlPullParserException e1 )
		{
			// TODO Auto-generated catch block
			e1.printStackTrace( );
		}
		while( eventType != XmlPullParser.END_DOCUMENT )
		{
			if( eventType == XmlPullParser.START_DOCUMENT ) // XmlPullParser.START_TAG
			                                                // or
			                                                // XmlPullParser.END_TAG
			                                                // is also available
			{
				System.out.println( "Import begins" );
			}
			else if( eventType == XmlPullParser.TEXT )
			{
				// Store the data in the hashMap temporarily
				gpsData.add( xpp.getText( ) );
			}
			try
			{
				// Jump to the next entry
				eventType = xpp.next( );
			}
			catch( XmlPullParserException e )
			{
				// TODO Auto-generated catch block
				e.printStackTrace( );
			}
			catch( IOException e )
			{
				// TODO Auto-generated catch block
				e.printStackTrace( );
			}

			// Copy the data in the Datamodel
			// Is there a File, the size will be at least the number of the
			// variables in the datamodel.java file
			if( gpsData.size( ) > 6 )
			{
				data.setCarLocation( Double.parseDouble( gpsData.get( 0 ) ), Double.parseDouble( gpsData.get( 1 ) ) );
				data.setHomeLocation( Double.parseDouble( gpsData.get( 2 ) ), Double.parseDouble( gpsData.get( 3 ) ) );
				data.setJobLocation( Double.parseDouble( gpsData.get( 4 ) ), Double.parseDouble( gpsData.get( 5 ) ) );
				data.setMailAddress( gpsData.get( 6 ) );
			}

		}

	}

}
